<?php

namespace app\dao\posts;

use app\dao\BaseDao;
use app\model\posts\Posts;

/**
 * 帖子
 * Class PostsDao
 * @package app\dao\posts
 */
class PostsDao extends BaseDao
{
    /**
     * 设置模型
     * @return string
     */
    protected function setModel(): string
    {
        return Posts::class;
    }

    public function search(array $where = [], bool $search = true)
    {
        return $this->getModel()->when(isset($where['user_id']) && $where['user_id'] != 0, function ($query) use ($where) {
            $query->where('user_id', $where['user_id']);
        })->when(isset($where['post_type']) && $where['post_type'] != 0, function ($query) use ($where) {
            $query->where('post_type', $where['post_type']);
        })->when(isset($where['status']), function ($query) use ($where) {
            $query->where('status', $where['status']);
        })->when(isset($where['nickname']) && $where['nickname'] != '', function ($query) use ($where) {
            $query->where('user_id', 'in', function ($que2) use ($where){
               $que2->name('user')->where('nickname', 'like', '%' . $where['nickname'] . '%')->field('uid')->select();
            });
        })->when(isset($where['is_del']), function ($query) use ($where) {
            $query->where('is_del', $where['is_del']);
        })->when(isset($where['id']) && $where['id'] != 0, function ($query) use ($where) {
            $query->where('id', $where['id']);
        });
    }
}